
public class byteUtil {
	public static short convShort(byte c){
		if(c>=0) return (short)c;
		return (short)(0x100+c);
	}
	public static byte convUByte(int c){
		if(c>0x7f) return (byte)(c-0x100);
		return (byte)c;
	}
	
	public static byte[] conv4UByte(int d){
		byte[] ret  = new byte[4];
		conv4UByte(d,ret);
		return ret;
	}
	public static void conv4UByte(int d,byte[] dest){
		conv4UByte(d,dest,0);
	}
	public static void conv4UByte(int d,byte[] dest,int startindex){
		int[] tmp = new int[4];
		tmp[0] = d / 0x1000000;
		d -= tmp[0]*0x1000000;
		tmp[1] = d / 0x10000;
		d -= tmp[1]*0x10000;
		tmp[2] = d / 0x100;
		d -= tmp[2]*0x100;
		tmp[3] = d ;
		
		dest[startindex+0] = convUByte(tmp[0]);
		dest[startindex+1] = convUByte(tmp[1]);
		dest[startindex+2] = convUByte(tmp[2]);
		dest[startindex+3] = convUByte(tmp[3]);
	}
	
	public static void conv4Byte(int d,byte[] dest,int startindex){
		dest[startindex+0] = convUByte((int) (0xff & (d>>(8*3))));
		dest[startindex+1] = convUByte((int) (0xff & (d>>(8*2))));
		dest[startindex+2] = convUByte((int) (0xff & (d>>(8*1))));
		dest[startindex+3] = convUByte((int) (0xff & (d>>(8*0))));
	}
	public static void conv3Byte(int d,byte[] dest,int startindex){
		dest[startindex+0] = convUByte((int) (0xff & (d>>(8*2))));
		dest[startindex+1] = convUByte((int) (0xff & (d>>(8*1))));
		dest[startindex+2] = convUByte((int) (0xff & (d>>(8*0))));
	}
	public static void conv2Byte(int d,byte[] dest,int startindex){
		dest[startindex+0] = convUByte((int) (0xff & (d>>(8*1))));
		dest[startindex+1] = convUByte((int) (0xff & (d>>(8*0))));
	}
	public static void convByte(int d,byte[] dest,int startindex){
		dest[startindex+0] = convUByte((int) (0xff & (d>>(8*0))));
	}
	public static void convHalfByte(int d,byte[] dest,int startindex,boolean lower){
		if(lower){
			dest[startindex+0] &= 0xf0;
			dest[startindex+0] |= (byte) (0x0f & (d));
		}
		else{
			dest[startindex+0] &= 0x0f;
			dest[startindex+0] |= (byte) (0xf0 & (d<<4));
		}
	}

	
	public static byte[] conv2UByte(int d){
		byte[] ret  = new byte[2];
		conv2UByte(d,ret);
		return ret;
	}
	public static void conv2UByte(int d,byte[] dest){
		conv2UByte(d,dest,0);
	}
	public static void conv2UByte(int d,byte[] dest,int startindex){
		int[] tmp = new int[2];
		tmp[0] = d / 0x100;
		d -= tmp[0]*0x100;
		tmp[1] = d ;
		
		dest[startindex+0] = convUByte(tmp[0]);
		dest[startindex+1] = convUByte(tmp[1]);
	}

	
	
	public static int valueOfHalfByte(byte[] c,int offset,boolean hidigit){
		short s =unsigned_byte.convShort(c[offset+0]);
		if(hidigit){
			s = (short)((s>>4) & 0xf);
		}
		else{
			s = (short)(s & 0xf);
		}
		int ret;
		if(s>=8){
			ret = (int)(s - 0x10);
		}
		else{
			ret = (int)s;
		}
		return ret;
	}
	public static int valueOf1Byte(byte[] c,int offset){
		int ld = unsigned_byte.convShort(c[offset+0]);
		int ret;
		if(ld >= 0x80){
			ret = (int)(ld - 0x100);
		}
		else{
			ret = (int)ld;
		}
		return ret;
	}
	public static int valueOf2Byte(byte[] c,int offset){
		int ld = unsigned_byte.convShort(c[offset+0])*0x100 + unsigned_byte.convShort(c[offset+1]); 
		int ret;
		if(ld >= 0x8000){
			ret = (int)(ld - 0x10000);
		}
		else{
			ret = (int)ld;
		}
		return ret;
	}
	public static int valueOf3Byte(byte[] c,int offset){
		int ld = unsigned_byte.convShort(c[offset+0])*0x10000 + unsigned_byte.convShort(c[offset+1])*0x100 
		+ unsigned_byte.convShort(c[offset+2]);
		int ret;
		if(ld >= 0x800000){
			ret = (int)(ld - 0x1000000);
		}
		else{
			ret = (int)ld;
		}
		return ret;
	}
	/**
	 * 
	 * @param c
	 * @param offset
	 * @return
	 */
	public static int valueOf4Byte(byte[] c,int offset){
		long ld = unsigned_byte.convShort(c[offset+0])*0x1000000 + unsigned_byte.convShort(c[offset+1])*0x10000 
		+ unsigned_byte.convShort(c[offset+2])*0x100 + unsigned_byte.convShort(c[offset+3]);
		int ret;
		if(ld >= 0x80000000L){
			ret = (int)(ld - 0x100000000L);
		}
		else{
			ret = (int)ld;
		}
		return ret;
	}
	static short convUShort(byte c){
		if(c>=0) return (short)c;
		return (short)(0x100+c);
	}
	
	public static void main(String[] arg){
//		byte[] buf = new byte[8];
//		
//		int[] dat = {0,1,-1,-2,-32765,0x7fff,0x8000,0xffff,0x10000};
//		System.out.println("conv2Byte( , ,3)");
//		for(int i=0;i<dat.length;i++){
//			conv2Byte(dat[i],buf,3);
//			printout(dat[i],buf);
//		}
//		
//		int[] dat2 = {0,1,-1,-2,-32765,0x7fffffff,0x80000000,-2147483647,0x32f10070};
//		for(int i=0;i<buf.length;i++) buf[i] = 0;
//		System.out.println("conv4Byte( , ,2)");
//		for(int i=0;i<dat2.length;i++){
//			int d = dat2[i];
//			conv4Byte(d,buf,2);
//			printout(d,buf);
//		}
//		int[] dat3 = {0,1,-1,-2,-7,-8,-9,6,7,8,9};
//		for(int i=0;i<buf.length;i++) buf[i] = 0;
//		System.out.println("convHalfByte( , ,1,false)|convHalfByte( , ,1,true)");
//		for(int i=0;i<dat3.length;i++){
//			int d = dat3[i];
//			convHalfByte(d,buf,1,false);
//			printout(d,buf);
//			convHalfByte(d,buf,1,true);
//			printout(d,buf);
//		}
		
	}
	private static void printout(int dat,byte[] buf){
		String mes = new String();
		mes = String.format("%d %x : %02x %02x %02x %02x-%02x %02x %02x %02x" ,dat,dat, buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6],buf[7]);
		System.out.println(mes);
	}
	
	
}
